setwd('')

library(fixest)
library(sandwich)
library(lmtest)
library(lmerTest)
library(texreg)

# Replication files: Door-to-door campaigns in an electoral autocracy: Evidence from Hungary

d2df <- read.csv("d2d_full.csv")
d2df_to_max <- read.csv("d2d_full_nooutlier.csv")
d2df_r <- read.csv("d2d_restricted.csv")
d2df_to_max_r <- read.csv("d2d_restricted_nooutlier.csv")
match_dat1 <- read.csv("d2d_match_nooutlier.csv")
match_dat2 <- read.csv("d2d_match.csv")
match_dat3 <- read.csv("d2d_match_noturnout.csv")

# ANALYSIS

# Main models, Column 1-4 (Table 1 and Appendix F, Table OA2)
mod1 <- lm(momentum_share_19 ~ visited, data= d2df_to_max)

mod2 <- lm(momentum_share_19 ~ visited + log_vpb + momentum_share_18 + 
             other_share_18 + fidesz_share_18 + turnout_18, data= d2df_to_max)

mod3 <- feols(momentum_share_19 ~ visited + log_vpb + momentum_share_18 + 
                other_share_18 + fidesz_share_18 + turnout_18 | city, data= d2df_to_max_r)

mod4 <- lm(momentum_share_19 ~ visited, data=match_dat1, weights=weights)

screenreg(list(mod1, mod2, mod3, mod4))
coeftest(mod4, vcov.=vcovCL, cluster=~subclass) ## adjusted SEs for matched model (not in screenreg table!)

# Main models Column 5-8 (Table 1 and Appendix F Table OA2) 
mod5 <- feols(other_share_19 ~ visited + log_vpb + momentum_share_18 + 
                fidesz_share_18 + other_share_18 + turnout_18| city, data=d2df_to_max_r)
mod6 <- lm(other_share_19 ~ visited, data=match_dat1, weights=weights)

mod7 <- feols(fidesz_share_19 ~ visited + fidesz_share_18  + momentum_share_18 +
                log_vpb + turnout_18 + other_share_18 | city, data = d2df_to_max_r)
mod8 <- lm(fidesz_share_19 ~ visited, data = match_dat1, weights=weights)

screenreg(list(mod5, mod6, mod7, mod8))
coeftest(mod6, vcov.=vcovCL, cluster=~subclass) ## adjusted SEs for matched model (not in screenreg table!)
coeftest(mod8, vcov.=vcovCL, cluster=~subclass) ## adjusted SEs for matched model (not in screenreg table!)

## Appendix

## Effect on Jobbik vs left opposition (Appendix G, Table OA3)
left_1 <- feols(left_share_19 ~ visited + log_vpb + momentum_share_18 + 
                  fidesz_share_18 + other_share_18 + turnout_18| city, data=d2df_to_max_r)
left_2 <- lm(left_share_19 ~ visited, data=match_dat1, weights=weights)

jobbik_1 <- feols(jobbik_share_19 ~ visited + fidesz_share_18  + momentum_share_18 +
                    log_vpb + turnout_18 + other_share_18 | city, data = d2df_to_max_r)
jobbik_2 <- lm(jobbik_share_19~ visited, data = match_dat1, weights=weights)

screenreg(list(left_1, left_2, jobbik_1, jobbik_2))
coeftest(left_2, vcov.=vcovCL, cluster=~subclass)
coeftest(jobbik_2, vcov.=vcovCL, cluster=~subclass)

## Robustness checks (Appendix H)

#Vote share by eligible voters
#Table OA4, Column 1-4
rob1_1 <- lm(momentum_share2_19 ~ visited, data= d2df_to_max)

rob1_2 <- lm(momentum_share2_19 ~ visited + log_vpb + momentum_share_18 + 
               other_share_18 + fidesz_share_18 + turnout_18, data= d2df_to_max)

rob1_3 <- feols(momentum_share2_19 ~ visited + log_vpb + momentum_share_18 + 
                  other_share_18 + fidesz_share_18 + turnout_18 | city, data= d2df_to_max_r)

rob1_4 <- lm(momentum_share2_19 ~ visited, data=match_dat1, weights=weights)

screenreg(list(rob1_1, rob1_2, rob1_3, rob1_4))
coeftest(rob1_4, vcov.=vcovCL, cluster=~subclass)

#Table OA4, Column 5-8
rob1_5 <- feols(other_share2_19 ~ visited + log_vpb + momentum_share_18 + 
                  fidesz_share_18 + other_share_18 + turnout_18| city, data=d2df_to_max_r)

rob1_6 <- lm(other_share2_19 ~ visited, data=match_dat1, weights=weights)

rob1_7 <- feols(fidesz_share2_19 ~ visited + fidesz_share_18  + momentum_share_18 +
                  log_vpb + other_share_18 + turnout_18 | city, data = d2df_to_max_r)

rob1_8 <- lm(fidesz_share2_19 ~ visited, data = match_dat1, weights=weights)

screenreg(list(rob1_5, rob1_6, rob1_7, rob1_8))
coeftest(rob1_6, vcov.=vcovCL, cluster=~subclass)
coeftest(rob1_8, vcov.=vcovCL, cluster=~subclass)

## Local election district (city_tevk) as fixed-effect (Table OA5)
rob2_1 <- feols(momentum_share_19 ~ visited + log_vpb + momentum_share_18 + 
                  other_share_18 + fidesz_share_18 + turnout_18 | 
                  city_tevk, data= d2df_to_max_r)

rob2_2 <- feols(other_share_19 ~ visited + log_vpb + momentum_share_18 + 
                  other_share_18 + fidesz_share_18 + turnout_18 | 
                  city_tevk, data= d2df_to_max_r)

rob2_3 <- feols(fidesz_share_19 ~ visited + log_vpb + momentum_share_18 + 
                  other_share_18 + fidesz_share_18 + turnout_18 | 
                  city_tevk, data= d2df_to_max_r)

screenreg(list(rob2_1, rob2_2, rob2_3))


# With outliers, without turnout 2018 as control (Appendix I, Table OA6)

##full
add1 <- lm(momentum_share_19 ~ visited, data= d2df)

## 2. full, covs
add2 <- lm(momentum_share_19 ~ visited + log_vpb + momentum_share_18 + 
             other_share_18 + fidesz_share_18, data= d2df)
add3 <- lm(momentum_share_19 ~ visited + log_vpb + momentum_share_18 + 
             other_share_18 + fidesz_share_18 + turnout_18, data= d2df)

## 3. restricted, town FEs, covs
add4 <- feols(momentum_share_19 ~ visited + log_vpb + momentum_share_18 + 
                other_share_18 + fidesz_share_18 | city, data= d2df_r)
add5 <- feols(momentum_share_19 ~ visited + log_vpb + momentum_share_18 + 
                other_share_18 + fidesz_share_18 + turnout_18 | city, data= d2df_r)

## 4. matching
add6 <- lm(momentum_share_19 ~ visited, data=match_dat2, weights=weights)
add7 <- lm(momentum_share_19 ~ visited, data=match_dat3, weights=weights)

screenreg(list(add1, add2, add3, add4, add5, add6, add7))
coeftest(add6, vcov.=vcovCL, cluster=~subclass)
coeftest(add7, vcov.=vcovCL, cluster=~subclass)

## END OF SCRIPT